/*module:view

---------------------------------------------------------------------------------------------------*/
define(function(){
	var myChart;
	var myChart2;
	var data={
		tooltip : {
			trigger: 'axis'
		},
		toolbox: {
			show : true,
			feature : {
				mark : false,
				dataZoom : false,
				dataView : true,
				magicType:['line'],
				restore : true,
				saveAsImage : true
			}
		},
		legend:{},
		xAxis:[{type:'category',data:[]}],
		yAxis:[{type:"value",splitArea : {show : true}}],
		series:[{name:"",type:"line",data:[]}],
		calculable : true
	};

	function chartInit(dom,callback){
		if(myChart == undefined){
			require.config({
				paths:{ 
					echarts:'http://img.itools.hk/bootstrap/echarts/echarts',
					'echarts/chart/bar' : 'http://img.itools.hk/bootstrap/echarts/echarts',
					'echarts/chart/line': 'http://img.itools.hk/bootstrap/echarts/echarts'
				}
			});

			require(
				[
					'echarts',
					'echarts/chart/line',
					'echarts/chart/bar'
				],
				function(ec) {
					myChart = ec.init(document.getElementById(dom));
					callback();
				}
			);
		}else{
			callback();
		}
	}


	return {
		createChart:function(dom,obj){
			chartInit(dom,function(){
				var _tit = [];
				var _arr =[];
				var n=0;
				for(var i in obj.xdata){
					_tit.push(i);
					_arr.push({
						name:i,
						type:(obj.linetype == undefined ? "line" : obj.linetype[n]),
						data:obj.xdata[i]
					});
					n++;
				}
				
				data.legend.data = _tit;
				data.series=_arr;
				data.xAxis=[{
					type : 'category',
					data : obj.timeline
				}];
				data.yAxis=[{
					type : 'value',
					scale: false,
					splitNumber:10,
					splitArea : {show : true}
				}];
				try{
					console.log("charts Data");
					console.log(data);
				}catch(e){}
				myChart.setOption(data,true);
			});
		},
		clearChart:function(){
			data.legend.data = [];
			data.series = [];
			myChart.clear();
		},
		createBlock:function(dom,obj){
			var _timeline = obj.timeline;
			var _xdata = obj.xdata;
			var _alltable = "<tr><th></th><th colspan=7>留存率</th></tr><tr><th>首次使用时间</th><th>1天后</th><th>2天后</th><th>3天后</th><th>4天后</th><th>5天后</th><th>6天后</th><th>7天后</th></tr>";
			var _innerHTML="";
			for(var i=0,max=_timeline.length;i<max;i++){
				_innerHTML += "<tr><td>"+_timeline[i]+"</td>";
				for(var j=0,max2=7;j<max2;j++){
					var n = _xdata[_timeline[i]][j];
					var css = "background:rgba(0,255,255,"+ n/1000+")";
					n=n/10+"%";
					_innerHTML += "<td style='"+css+"'>"+n+"</td>";
				}
				_innerHTML += "</tr>";
			}
			_innerHTML = _alltable+_innerHTML;
			$(dom).html(_innerHTML);
		},
		createChartForTimedist:function(dom,obj){
			var _tit = ["时长"];
			var _x = [];
			var _y = [];
			for(var i=0,max=obj.length;i<max;i++){
				_x.push(obj[i].time);
				_y.push(obj[i].percent/10);
			}
			data.legend.data = _tit;
			data.series=[{
				name:"时长",
				type:"bar",
				data:_y
			}];
			data.xAxis=[{
				type : 'category',
				data : _x
			}];
			data.yAxis=[{
				type : 'value',
				scale: false,
				splitNumber:10,
				axisLabel : {
                			formatter: '{value} %'
            			},
				splitArea : {show : true}
			}];
			try{
				console.log("charts Data");
				console.log(data);
			}catch(e){}
			myChart.setOption(data,true);
		},
		createChartForDlapp:function(dom,obj){
			
			data.legend.data = ["免费正版下载速度","官方下载速度"];
			data.xAxis=[{
				type : 'category',
				data : obj.timeline
			}];
			var _avg_f_spd = [];
			var _avg_s_spd = [];
			var fail_free = [];
			var suc_free = [];
			var update = [];
			var active = [];
			var fail_store = [];
			var suc_store = [];
			for(var i =0,max=obj.timeline.length;i<max;i++){
				_avg_f_spd.push(obj.xdata[obj.timeline[i]]["avg_f_spd"]/1000);
				_avg_s_spd.push(obj.xdata[obj.timeline[i]]["avg_s_spd"]/1000);
			}
			data.series = [{
				name:"免费正版下载速度",
				type:"line",
				data:_avg_f_spd
			},{
				name:"官方下载速度",
				type:"line",
				data:_avg_s_spd
			}];
			data.yAxis=[{
				type : 'value',
				scale: false,
				splitNumber:10,
				axisLabel : {
                			formatter: '{value} kb/s'
            			},
				splitArea : {show : true}
			}];
			try{
				console.log("charts Data");
				console.log(data);
			}catch(e){}
			myChart.setOption(data,true);
		},
		createChartForDlappcount:function(dom,obj){

			data.legend.data = ["免费下载失败","免费下载成功","更新量","下载量","官方下载失败","官方下载成功"];
			data.xAxis=[{
				type : 'category',
				data : obj.timeline
			}];

			var fail_free = [];
			var suc_free = [];
			var update = [];
			var active = [];
			var fail_store = [];
			var suc_store = [];
			for(var i =0,max=obj.timeline.length;i<max;i++){
				fail_free.push(obj.xdata[obj.timeline[i]]["fail_free"]);
				suc_free.push(obj.xdata[obj.timeline[i]]["suc_free"]);
				update.push(obj.xdata[obj.timeline[i]]["update"]);
				active.push(obj.xdata[obj.timeline[i]]["active"]);
				fail_store.push(obj.xdata[obj.timeline[i]]["fail_store"]);
				suc_store.push(obj.xdata[obj.timeline[i]]["suc_store"]);
			}
			data.series = [{
				name:"免费下载失败",
				type:"bar",
				stack:"free",
				data:fail_free
			},{
				name:"免费下载成功",
				type:"bar",
				stack:"free",
				data:suc_free
			},{
				name:"更新量",
				type:"bar",
				stack:"下载",
				data:update
			},{
				name:"下载量",
				type:"bar",
				stack:"下载",
				data:active
			},{
				name:"官方下载失败",
				type:"bar",
				stack:"官方",
				data:fail_store
			},{
				name:"官方下载成功",
				type:"bar",
				stack:"官方",
				data:suc_store
			}];
			data.yAxis=[{
				type : 'value',
				scale: false,
				splitNumber:10,
				axisLabel : {
                			formatter: '{value}'
            			},
				splitArea : {show : true}
			}];
			try{
				console.log("charts Data");
				console.log(data);
			}catch(e){}
			myChart.setOption(data,true);
		}
	}
});